/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package model

import "gitee.com/tugoer/orivil/bundles/db"

type WeChat struct {
	ID int
	AdminID int `gorm:"index"`
	Name string
	MPFile string
	MPText string
	AppID string `gorm:"index"`
	Secret string
}

// admin 必须为登陆的管理员 id
func (w *WeChat)Create(admin int) error {
	w.AdminID = admin
	return db.Gorm.Create(w).Error
}

// admin 必须为登陆的管理员 id
func (w *WeChat)Update(admin int) error {
	return db.Gorm.Model(WeChat{}).Where("id=? AND admin_id=?", w.ID, admin).Updates(w).Error
}

// admin 必须为登陆的管理员 id
func QueryWeChat(admin int) (ws []*WeChat) {
	db.Gorm.Where("admin_id=?", admin).Order("id desc").Find(&ws)
	return ws
}

func GetWeChatByAppID(appid string) *WeChat {
	w := &WeChat{}
	db.Gorm.Where("app_id=?", appid).First(w)
	return w
}

func GetWeChatByID(id int) *WeChat {
	w := &WeChat{ID: id}
	db.Gorm.First(w)
	return w
}

// admin 必须为登陆的管理员 id
func (w *WeChat) Delete(admin int) (err error) {
	return db.Gorm.Where("id=? AND admin_id=?", w.ID, admin).Delete(w).Error
}